import { Injectable } from '@nestjs/common';
import { InjectRedis } from '@nestjs-modules/ioredis';
import Redis from 'ioredis';

@Injectable()
export class RedisCommonService {
  redisClient: Redis;
  constructor(@InjectRedis() private readonly redis: Redis) {
    this.redisClient = new Redis(this.redis.options);
  }

  async set(key: string, value: string): Promise<void> {
    await this.redis.set(key, value);
  }

  async get(key: string): Promise<string | null> {
    return await this.redis.get(key);
  }

  async apiSet(key: string, value: string, ttl: number = 60): Promise<void> {
    await this.redis.set(key, value, 'EX', ttl);
  }

  async apiGet(key: string): Promise<string | null> {
    return await this.redis.get(key);
  }

  getClient(): Redis {
    return this.redisClient;
  }
}
